import { SCORE_RULE } from './config'

export class NewEnergyScore {
    //初始分数
    totalScore = 100
    init(batteryDetails: any) {
        for (const key in SCORE_RULE) {
            if (isNaN(Number(batteryDetails[key]))) {
                const result: (number | string)[] = batteryDetails[key].split('-')
                if (Object.prototype.toString.call(SCORE_RULE[key]) === '[object Object]') {
                    if ('max' in SCORE_RULE[key]) {
                        const s = result.some(i => {
                            return Number(i) > SCORE_RULE[key].max
                        })
                        if (s) {
                            this.totalScore -= SCORE_RULE[key].deduct
                        }
                        console.log(
                            key,
                            SCORE_RULE[key],
                            batteryDetails[key],
                            result,
                            '减去',
                            s ? SCORE_RULE[key].deduct : 0
                        )
                    } else if ('min' in SCORE_RULE[key]) {
                        const s = result.some(i => {
                            return Number(i) < SCORE_RULE[key].min
                        })
                        if (s) {
                            this.totalScore -= SCORE_RULE[key].deduct
                        }
                        console.log(
                            key,
                            SCORE_RULE[key],
                            batteryDetails[key],
                            result,
                            '减去',
                            s ? SCORE_RULE[key].deduct : 0
                        )
                    }
                }
            } else {
                if (Object.prototype.toString.call(SCORE_RULE[key]) === '[object Object]') {
                    if ('max' in SCORE_RULE[key]) {
                        const s = Number(batteryDetails[key]) > SCORE_RULE[key].max ? true : false
                        if (s) {
                            this.totalScore -= SCORE_RULE[key].deduct
                        }
                        console.log(key, SCORE_RULE[key], batteryDetails[key], '减去', s ? SCORE_RULE[key].deduct : 0)
                    } else if ('min' in SCORE_RULE[key]) {
                        const s = Number(batteryDetails[key]) < SCORE_RULE[key].min ? true : false
                        if (s) {
                            this.totalScore -= SCORE_RULE[key].deduct
                        }
                        console.log(key, SCORE_RULE[key], batteryDetails[key], '减去', s ? SCORE_RULE[key].deduct : 0)
                    }
                } else if (Object.prototype.toString.call(SCORE_RULE[key]) === '[object Array]') {
                    let result = 0
                    const s = SCORE_RULE[key].some((item: any) => {
                        let r = false
                        if ('range' in item) {
                            if (Object.prototype.toString.call(item.range) === '[object Array]') {
                                r =
                                    item.range[0] <= Number(batteryDetails[key]) &&
                                    item.range[1] > Number(batteryDetails[key])
                                        ? true
                                        : false
                                if (r) {
                                    result = item.deduct
                                }
                            } else if (Object.prototype.toString.call(item.range) === '[object Object]') {
                                if ('max' in item.range) {
                                    r = item.range.max < Number(batteryDetails[key]) ? true : false
                                    if (r) {
                                        result = item.deduct
                                    }
                                } else if ('min' in item.range) {
                                    r = item.range.min > Number(batteryDetails[key]) ? true : false
                                    if (r) {
                                        result = item.deduct
                                    }
                                }
                            }
                        } else {
                            if ('max' in item) {
                                if (!r) {
                                    r = Number(batteryDetails[key]) > item.max ? true : false
                                    result = r ? item.deduct : 0
                                }
                            } else if ('min' in item) {
                                if (!r) {
                                    r = Number(batteryDetails[key]) < item.min ? true : false
                                    result = r ? item.deduct : 0
                                }
                            }
                            // console.log(key, SCORE_RULE[key], Number(batteryDetails[key]), '减去', result)
                        }
                        return r
                    })
                    if (s) {
                        this.totalScore -= result
                    }

                    console.log(key, SCORE_RULE[key], Number(batteryDetails[key]), '减去', result)
                }
            }
        }
        return this.totalScore
    }
}
